home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / sum101.zip / SUM.C < prev    next >
Text File  |  1989-03-26  |  1KB  |  64 lines

  1. /* SUM implied "-r" option. */
  2.  
  3. #include <stdio.h>
  4.  
  5. #define BUFSIZE 512
  6.  
  7. main (argc, argv) int argc; char *argv[]; {
  8. int    i, c;
  9. char     *tmpptr;
  10. char     TextMode;
  11. unsigned int    sum, sumsave;
  12. long    nbytes;
  13. FILE    *inp;
  14.  
  15. sum = sumsave = nbytes = 0;
  16. TextMode = 1;
  17.  
  18. i=1;
  19. if (i<argc) {
  20.     tmpptr = argv[i];
  21.     if (*tmpptr=='-') {
  22.         ++tmpptr;
  23.         if (*tmpptr=='b')
  24.             TextMode = 0;
  25.         else
  26.             goto BADPARAMS;
  27.     i++;
  28.     }
  29. }
  30.  
  31. if (i<argc) {
  32.     if ( (inp=fopen(argv[i], "rb")) == NULL ) {
  33.         printf ("\ninput file %s open error\n", argv[i]);
  34.         goto BADPARAMS;
  35.     }
  36. }
  37.  
  38. if (++i!=argc) {
  39. BADPARAMS:
  40.     printf ("\nsum [-b] input-file   (vers 1.01)\n");
  41.     printf ("\tcomputes UNIX compatible sum -r of input file\n");
  42.     printf ("\t-b   - binary mode, use all characters in file\n");
  43.     printf ("\telse - text mode, end of line is interpreted as LF (not CR-LF)\n");
  44.     return (1);
  45. }
  46.  
  47. while ( (c=fgetc(inp)) != EOF) {
  48.     if (TextMode) {
  49.         if (c==0x1A)
  50.             break;
  51.         if ( (sumsave!=0) && (c==0x0A) ) {
  52.             sum = sumsave;
  53.             nbytes--;
  54.         }
  55.         sumsave = (c==0x0D) ? sum : 0;
  56.     }
  57.     /* cyclic right shift 1 of sum + char read in */
  58.     nbytes++;
  59.     sum = ( (sum>>1) + ( (sum & 1)?0x8000:0) + c ) & 0xffff;
  60. }
  61. fclose(inp);
  62. printf ("%5u %ld\n", sum, nbytes);
  63. return (0);
  64. }